/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.controller;

import astrologer.validator.EncodePassword;
import com.aptech.controller.util.JsfUtil;
import com.aptech.entity.Member1;
import com.aptech.session.Member1FacadeLocal;
import com.aptech.session.RolesFacadeLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Tuan Nguyen
 */
@ManagedBean
@SessionScoped

public class UserManager{
    @EJB
    private RolesFacadeLocal rolesFacade;

    @EJB
    private Member1FacadeLocal member1Facade;
    private String email;
    private String password;
    private int UserID;
    private Member1 user;
    
    public int getUserID() {
        return UserID;
    }

    public void setUserID(int UserID) {
        this.UserID = UserID;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
    private String role;

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Member1 getUser() {
        return user;
    }

    public void setUser(Member1 user) {
        this.user = user;
    }

    /** Creates a new instance of UserManager */
    public UserManager() {
    }

    public String login() {
        FacesContext context = FacesContext.getCurrentInstance();
        if (member1Facade.findByEmail(email).size() > 0) {
            user = member1Facade.findByEmail(email).get(0);
            if (!user.getPassword().equals(EncodePassword.getEncodePassword(password))) {
                JsfUtil.addErrorMessage("Login Failed! "
                        + "The password specified is not correct.");
                return null;
            }
        } else {
            JsfUtil.addErrorMessage("Login Failed! Username '" + email + "' does not exist.");
            return null;
        }
        role = rolesFacade.find(user.getRoleID().getRoleID()).getRoleName();
        return "successfully";
    }

    public String logout() {
        user = null;
        return "Login";
    }

    public String ridirect() {
        return "Login";
    }

    public void userResponse() throws IOException {
        String uri = "../Login.xhtml";
        FacesContext.getCurrentInstance().getExternalContext().dispatch(uri);
    }

    public boolean getIsAdmin() {
        return (getRole() != null && getRole().equals("Admin"));
    }
    public String changePass(){
        return "Sucessfully";
    }
    public void userResponse1() throws IOException {
        String uri = "Login.xhtml";
        FacesContext.getCurrentInstance().getExternalContext().dispatch(uri);
    }
}
